package cn.edu.gzhu.workOrderManagement.Fillter;


import cn.edu.gzhu.workOrderManagement.wrapper.CachedBodyHttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 这是一个过滤器用于多次读取请求体的内容
 * @author chen xi
 */
@Slf4j
@WebFilter(urlPatterns ={"/user/*","/organization/*","/workOrder/*"})
public class IMTAFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (servletRequest instanceof HttpServletRequest) {
            HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
            String method = httpRequest.getMethod();

            // 仅缓存 POST 请求的请求体
            if ("POST".equalsIgnoreCase(method)) {
                ServletRequest requestWrapper = new CachedBodyHttpServletRequestWrapper(httpRequest);
                filterChain.doFilter(requestWrapper, servletResponse);
                return;
            }
        }

        // 其他请求方法（如 GET、PUT、DELETE）直接放行
        filterChain.doFilter(servletRequest, servletResponse);
    }
}

